<?php
namespace App\Http\Controllers\Service;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\ApiData;
use App\Models\V1\RegionModel;




class Region{


    private $region;
    private $apiData;
    public $error;

    public function __construct(){
        $this->region   = new RegionModel();
        $this->apiData  = new ApiData();
    }


    /**
     * 获得省份列表
     *
     * */
    public function get_provinces(){
        $provinces  = $this->region->get_provices();
        return $this->apiData->set_data('provinces',$provinces)->send(200,'ok');
    }


    /**
     * 获取城市
     * @param Request $request 参数列表
     * @return Object;
     * */
    public function get_cities(Request $request){
        $provinceId = $request->input('provinceId');
        $cities     = $this->region->get_citys($provinceId);
        return $this->apiData->set_data('cities',$cities)->send(200,'ok');
    }


    /**
     * 找到手机号的城市ID
     * @param string $mobile 手机号
     * @return ApiData
     * */
    public function get_mobile_city_id($mobile = ""){
        if(!$mobile){
            $request    = new Request();
            $mobile = $request->input('mobile');
        }

        $regionInfo = $this->get_mobile_city_info($mobile);

        if($regionInfo){
            return $this->apiData->set_data('regionInfo',$regionInfo)->send(200,'ok');
        }else{
            return $this->apiData->send(4002,$this->error);
        }
    }



    /**
     * 获取城市的信息
     * @param string $mobile 手机号
     * @return Array|boolean
     * */
    public function get_mobile_city_info($mobile){

        $info   = $this->get_mobile_area($mobile);

        if($info == 1){
            return false;
        }

        $info   = \GuzzleHttp\json_decode($info);
        if($info->status != 0){ //获取失败
            $this->error = $info->msg;
            return false;
        }

        //找到城市
        $mobileInfo = $info->result;
        $city       = $mobileInfo->city;
        if(empty($city)){
            $city = $mobileInfo->province;
        }


        $regionInfo = DB::table('region')
            ->where('region_name',$city)
            ->where('parent_id',"<>",1)
            ->first();
        if(!$regionInfo){
            $city   = "%$city%";
            $regionInfo = DB::table('region')
                ->where('region_name','like',$city)
                ->where('parent_id',"<>",1)
                ->first();

        }

        if(!$regionInfo){
            $this->error = "系统不存在对应的归属地";
        }
        return $regionInfo;
    }

    /**
     * 获得手机号的地域信息
     * */
    public function get_mobile_area($mobile){
        #$url = "http://doc.jdclo.com/websvn/test.php?mobile=".$mobile;
        $url = "http://doc.jdclo.com/websvn/mobile.php?mobile=".$mobile;

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;


        //以下暂时不适用
        //======================
        $host = "http://jshmgsdmfb.market.alicloudapi.com";
        $path = "/shouji/query";
        $method = "GET";
        $aliyunConfig   =  config('aliyun');
        $appcode = $aliyunConfig['AppCode'];
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "shouji=".$mobile;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);


        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        $response = curl_exec($curl);
        mylogger($response);
        var_dump($response);
        exit;
        $result = $this->format_curl_response($response,$curl);
        curl_close($curl);
        return $result['body'];
    }


    public function get_mobile_area1($mobile){
        $host = "http://showphone.market.alicloudapi.com";
        $path = "/6-1";
        $method = "GET";
        $appcode = "e787fe38390a48a381b48fa833e91e23";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "num=".$mobile;
        $bodys = "";
        $url = $host . $path . "?" . $querys;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);

        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }

        $res = curl_exec($curl);
        var_dump($res);
        exit;
    }


    //格式化返回的数据
    private function format_curl_response($response,&$curl){
        $header_size    = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
        //$header_size = curl_getinfo($curl);

        $result['header'] = substr($response, 0, $header_size);

        $result = curl_getinfo($curl, CURLINFO_HEADER_OUT);
        mylogger($result);
        exit;

        $result['body'] = substr( $response, $header_size );
        $result['http_code'] = curl_getinfo($curl,CURLINFO_HTTP_CODE);
        $result['last_url'] = curl_getinfo($curl,CURLINFO_EFFECTIVE_URL);
        return $result;
    }

    public function get_countrys(){
        $regionModel    = new RegionModel();
        $countrys       = $regionModel->get_countries();
        return apiData()->set_data('countrys',$countrys)->send(200,'ok');
    }
}